from odoo import api, fields, models
from ..exception.customer_exception import CustomerException
from ..exception import global_exception_handler


class FastapiEndpoint(models.Model):
    _inherit = "fastapi.endpoint"
    app: str = fields.Selection(
        selection_add=[("Test API", "自动化测试接口")], ondelete={"Test API": "cascade"}
    )

    def _get_fastapi_routers(self):
        if self.app == "Test API":
            from ..controller.security_case_controller import router as security_case_router
            from ..controller.coupon_case_controller import router as coupon_case_router
            return [security_case_router,coupon_case_router]
        return super()._get_fastapi_routers()

    def _get_app(self):
        app = super()._get_app()
        if self.app == "Test API":
            app.version = "1.0.0"
        return app

    def _get_app_exception_handlers(self):
        handlers = super(FastapiEndpoint, self)._get_app_exception_handlers()
        handlers[CustomerException] = global_exception_handler.customer_exception_handler
        return handlers

    # def _get_app_exception_handlers(self):
    #     handlers = super()._get_app_exception_handlers()
    #     if self.app == "Web API":
    #         # 为特定端点添加或覆盖异常处理器
    #         handlers[HTTPException] = global_exception_handler
    #         handlers[Exception] = global_exception_handler
    #     return handlers
